const moduleMap = require('./moduleMap');

let historyStore = {
  setHistories( type, data ) {
    if ( !moduleMap[ type ] ) {
      console.error('error: set history data type error.');
      return;
    }
    if ( !data.length ) {
      console.warn('warn: type of saved is not Array.');
    }
    let key = 'history_' + moduleMap[ type ];
    wx.setStorage({
      key: key,
      data: data
    })
  },
  getHistories( type, callback ) {
    if ( !moduleMap[ type ] ) {
      console.error('error: set history data type error.');
      callback( [] );
      return;
    }
    let key = 'history_' + moduleMap[ type ];
    wx.getStorage({
      key: key,
      success( res ) {
        callback( res.data || [] );
      },
      fail() {
        callback( [] );
      }
    })
  },
  clearHistories( type, callback ) {
    let key = 'history_' + moduleMap[ type ];
    wx.removeStorage({
      key: key
    })
  }
}
module.exports = historyStore;